package com.xyzq.cgate.admin.bootstrap;

import com.alibaba.fastjson.JSON;
import com.xyzq.cgate.common.rule.custom.entity.RequestData;
import okhttp3.*;

import java.io.IOException;

/**
 * @Description:
 * @author chenchen
 * @date 2019/6/17 11:02
 */
public class Bootstrap {

    public static final MediaType TYPE = MediaType.parse("application/json;charset=utf-8");

    public boolean bootstrapSyncPlugin(String hostAddress, Integer serverPort) {
        String url = "http://" + hostAddress + ":" + serverPort + "/admin/plugin/sync";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    public boolean bootstrapSyncRule(String hostAddress, Integer serverPort, RequestData requestData) {
        String url = "http://" + hostAddress + ":" + serverPort + "/admin/rule/sync";
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(TYPE, JSON.toJSONString(requestData));
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

}
